﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.WebRes.Data
{
    public partial class ResType
    {
        public static Dictionary<int, ResType> Map = new Dictionary<int, ResType>();

        public static ResType None    = new ResType(1, "未指定",	"None",	    null,	"未指定，通常用于父栏目"	,1);
        public static ResType Article = new ResType(2, "文章",	"Article",	null,	"文章类",	2);
        public static ResType Picture = new ResType(3, "图片",	"Picture",	"jpg,jpeg,gif,png,bmp",	"图片类",	3);
        public static ResType Media   = new ResType(4, "媒体",	"Media",	"avi,wav,rm,rmvb",	"视频类",	4);
        public static ResType Product = new ResType(5, "产品",   "Product",  null, "产品介绍类", 5);
        public static ResType File    = new ResType(6, "文件",	"File",	    "zip,rar,pdf,doc",	"下载类资源",	6);
        public static ResType Folder  = new ResType(7, "文件夹",	"Folder",	null,	"服务器端文件夹资源",	7);

        public static void Initialize(ResourceSysEntities db)
        {
            var list = db.ResTypeTBL.ToList();
            if (list.Count > 0)
            {
                list.ForEach(p =>
                {
                    Map[p.ID].Name = p.Name;
                    Map[p.ID].Abbr = p.Abbr;
                    Map[p.ID].Explain = p.Explain;
                    Map[p.ID].Ext = p.Ext;
                    Map[p.ID].OrderNo = p.OrderNo;
                });
            }
            else
            {
                Array.ForEach(Map.Values.ToArray(), p=>{
                    db.AddToResTypeTBL(p);
                });
            }
        }

        private ResType(int id, string name, string abbr, string ext, string explain, int orderNo)
        {
            this.ID = id;
            this.Name = name;
            this.Abbr = abbr;
            this.Explain = explain;
            this.Ext = ext;
            this.OrderNo = orderNo;

            Map.Add(id, this);
        }

        public ResType() { }

        public string GetUploadFolder()
        {
            if (this == ResType.Picture)
            {
                return SysConfig.UploadPictureFolder;
            }
            else if (this == ResType.File)
            {
                return SysConfig.UploadFileFolder;
            }
            else if (this == ResType.Media)
            {
                return SysConfig.UploadMediaFolder;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
